package com.geezdata.cps.sdk3rd.sms;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.exceptions.MyException;
import com.geezdata.cps.sdk3rd.sms.enums.ProviderEnum;
import com.geezdata.cps.sdk3rd.sms.provider.AXJProvider;
import com.geezdata.cps.sdk3rd.sms.provider.BaseProvider;
import com.geezdata.cps.sdk3rd.sms.provider.YTXProvider;
import com.geezdata.cps.sdk3rd.sms.request.BalanceRequest;
import com.geezdata.cps.sdk3rd.sms.request.SmsRequest;
import com.geezdata.cps.sdk3rd.sms.response.SmsResult;

public class SmsDefaultClient implements SmsClient {
	private static final Logger logger = LoggerFactory.getLogger(SmsDefaultClient.class);
	
    @Override
	public SmsResult sendSms(SmsRequest request) {
    	BaseProvider provider = createProvider(request.getProvider());
    	if (provider == null) {
    		logger.error("短信服务商无效");
    		throw new MyException(ErrorCode.HTTP_EXCEPTION.getCode(), "短信服务商无效");
    	}
    	
        return provider.sendSms(request);
    }

	@Override
	public Integer queryBalance(BalanceRequest request) {
		BaseProvider provider = createProvider(request.getProvider());
    	if (provider == null) {
    		logger.error("短信服务商无效");
    		throw new MyException(ErrorCode.HTTP_EXCEPTION.getCode(), "短信服务商无效");
    	}
    	
        return provider.queryBalance(request);
	}
	
	public BaseProvider createProvider(String provider) {
		if (StringUtils.isEmpty(provider)) {
			return null;
		}
		
		if(provider.equals(ProviderEnum.AXJ.getName())) {
			return new AXJProvider();
		}
		
		if(provider.equals(ProviderEnum.YTX.getName())) {
			return new YTXProvider();
		}
		
		return null;
	} 
}
